Home

Computer science

'

\ufeffThe second project involves modifying the syntactic analyzer for the attached compiler by
adding to the existing grammar. The full grammar of the language is shown below. The
highlighted portions of the grammar show what you must either modify or add to the existing
grammar.
function:
function_header {variable} \ufeffbody
function_header:
FUNCTION IDENTIFIER [parameters] \ufeffRETURNS type ;
variable:
IDENTIFIER : type IS statement ; |
IDENTIFIER : LIST OF type IS list ;
list:
( \ufeffexpression {, \ufeffexpression} )
parameters:
parameter {, \ufeffparameter}
parameter:
IDENTIFIER : type
type:
INTEGER | \ufeffREAL | \ufeffCHARACTER
body:
BEGIN statement END ;
statement:
expression ; |
WHEN condition , \ufeffexpression : expression ; |
SWITCH expression IS {case} \ufeffOTHERS ARROW statement ENDSWITCH ; |
IF condition THEN statement {ELSIF condition THEN statement}
ELSE statement ENDIF ; |
FOLD direction operator list_choice ENDFOLD ;
case:
CASE INT_LITERAL ARROW statement
direction:
LEFT | \ufeffRIGHT
operator:
ADDOP | \ufeffMULOP
list_choice:
list |
IDENTIFIER
condition:
expression RELOP expression |condition logical_operator condition |
( \ufeffcondition ) |
NOTOP condition
logical_operator:
ANDOP | \ufeffOROP
expression:
( \ufeffexpression ) |
expression arithmetic_operator expression |
NEGOP expression |
INT_LITERAL | \ufeffREAL_LITERAL | \ufeffCHAR_LITERAL |
IDENTIFIER ( \ufeffexpression ) |
IDENTIFIER
arithmetic_operator: ADDOP | \ufeffMULOP | \ufeffMODOP | \ufeffEXPOP
In the above grammar, the red symbols are nonterminals, the blue symbols are terminals and the
black punctuation are EBNF metasymbols. The braces denote repetition 0 \ufeffor more times and the
brackets denote optional.
You must rewrite the grammar to eliminate the EBNF brace and bracket metasymbols and to
incorporate the significance of parentheses, operator precedence and associativity for all
operators. The precedence and associativity rules are as follows:
\uf0b7
\uf0b7
\uf0b7
\uf0b7
Among binary arithmetic operators the exponentiation operator has highest precedence
followed by the multiplying operators and then the adding operators. But the unary
negation operator ~ has higher precedence that all of the binary operators.
All relational operators have the same precedence.
Among the binary logical operators, & has higher precedence than |. \ufeffBut the unary logical
operator ! \ufeffhas higher precedence than either of the binary logical operators.
All binary operators except the exponentiation operator are left associative.
The directives to specify precedence and associativity, such as %prec and %left, may not be
used.
Your parser should be able to correctly parse any syntactically correct program without any
problem.
You must modify the syntactic analyzer to detect and recover from additional syntax errors using
the semicolon as the synchronization token. To accomplish detecting additional errors an error
production must be added to the function body, to the variable declaration and to the when
clause.
Your bison input file should not produce any shift/reduce or reduce/reduce errors. Eliminating
them can be difficult so the best strategy is not introduce any. That is best achieved by making
small incremental additions to the grammar and ensuring that no addition introduces any such
errors.
An example of compilation listing output containing syntax errors is shown below:1 -- \ufeffMultiple errors
2
3 \ufefffunction main a integer returns real;
Syntax Error, Unexpected INTEGER, expecting \':\'
4
b: integer is * 2;
Syntax Error, Unexpected MULOP
5
c: real is 6.0;
6 \ufeffbegin
7
if a > \ufeffc then
8
+ / .4;
Syntax Error, Unexpected MULOP
9
else
10
case b is
11
when => 2;
Syntax Error, Unexpected ARROW, expecting INT_LITERAL
12
when 2 => \ufeffc;
13
endcase;
Syntax Error, Unexpected ENDCASE, expecting OTHERS or WHEN
14
endif;
15 \ufeffend;
Lexical Errors 0
Syntax Errors 5
Semantic Errors 0
'

Answer